﻿using MediatR;
using ProduceBasicData.Api.Application.Command.Workshop;
using ProduceBasicData.Domain.FactoryModeling;
using ProduceBasicData.ErrorCode;
using ProduceBasicData.Infrastructure.Interface;

namespace ProduceBasicData.Api.Application.Handler.Workshop
{
    public class DeleteRangeWorkshopHandler : IRequestHandler<DeleteRangeWorkshopCommand, ApiResult<int>>
    {
        private readonly IBaseRespository<Workshopmodel> shopRepo;

        public DeleteRangeWorkshopHandler(IBaseRespository<Workshopmodel> shopRepo)
        {
            this.shopRepo = shopRepo;
        }

        public async Task<ApiResult<int>> Handle(DeleteRangeWorkshopCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res = new ApiResult<int>();
            var shopIds = request.Ids.Split(',').Select(int.Parse).ToList();
            var shoplist = shopRepo.GetAll().Where(x => shopIds.Contains(x.Id)).ToList();

            foreach (var item in shoplist)
            {
                item.Isdel = true;
            }
            var r = await shopRepo.UpdateRange(shoplist);
            res.Data = r;
            res.Code = ApiEnum.Success;
            res.Msg = "删除成功";
            return res;
        }
    }
}
